<?php

class ProductController extends Controller
{	
	public function init(){
		parent::init();
		$cs = Yii::app()->clientScript;
		$cs->registerScriptFile('/js/jquery.js');
		$cs->registerScriptFile('/js/facebox.js');
		$cs->registerScriptFile('/js/tiny_mce/tiny_mce.js');
		$cs->registerScriptFile('/js/highcharts.js');
		$cs->registerScriptFile('jquery.sharrre-1.3.2.min');
	}
	public function actionIndex($id)
	{
		$this->layout = nil;
		$dr = Product::model()->productDetail($id);
		$this->renderPartial('_product', array('dr'=>$dr));
	}
	public function actionView($name, $id)
	{
		$store = Store::model()->findByAttributes(array('name'=>$name));
		$dr = Product::model()->productDetail($id);
		$this->render('view', array('store'=>$store, 'dr'=>$dr));
	}
	
	public function actionEdit($id){
		$product = Product::model()->findByPk($id);
		$media = Media::model()->findByPk($product->media_id);
		$old = $media->url_400;
		if(isset($_POST['Product']))
		{	
			$product->attributes=$_POST['Product'];
			$media->attributes=$_POST['Media'];
			$media->url_400 = CUploadedFile::getInstance($media,'url_400');
			$path = 'images/products/'.$product->id.'/';
			if(is_object($media->url_400)){
				$file_name = $media->url_400->getName();
				$media->url_110 = '110_'.$file_name;
				$media->url_80 = '80_'.$file_name;
			}
			else
				$media->url_400 = $old;
			if($product->save() && $media->save()){
				if(is_object($media->url_400)){
					$media->url_400->saveAs($path.$file_name);
					$image = Yii::app()->image->load($path.$file_name);
					$image->resize(350,350);
					$image->save();
					
					$image->resize(110,110);
					$image->save($path.'110_'.$file_name);
					
					$image->resize(80,80);
					$image->save($path.'80_'.$file_name);
				}
			}
			$this->redirect(array('store/index','name'=>$product->store->name));
		}
		$this->renderPartial('_edit', array('product'=>$product, 'media'=>$media));
	}
	
	public function actionAdd($store_id){
		$product = new Product;
		$product->store_id = $store_id;
		$media = new Media;
		
		if(isset($_POST['Product']))
		{	
			$media->attributes=$_POST['Media'];
			$media->url_400 = CUploadedFile::getInstance($media,'url_400');
			if(is_object($media->url_400)){
				$file_name = $media->url_400->getName();
				$media->url_110 = '110_'.$file_name;
				$media->url_80 = '80_'.$file_name;
			}
			if($media->save()){
				$product->media_id = $media->id;
				$product->attributes=$_POST['Product'];
				if($product->save()){
					$path = 'images/products/'.$product->id.'/';
					mkdir($path);
					$media->url_400->saveAs($path.$file_name);
					$image = Yii::app()->image->load($path.$file_name);
					$image->resize(350,350);
					$image->save();
					
					$image->resize(110,110);
					$image->save($path.'110_'.$file_name);
					
					$image->resize(80,80);
					$image->save($path.'80_'.$file_name);
				}
			}
			$this->redirect(array('store/index','name'=>$product->store->name));
		}
		$this->renderPartial('_edit', array('product'=>$product, 'media'=>$media));
	}
	public function actionDefault($store_id, $id)
	{	
		Yii::app()->db->createCommand("
				UPDATE store SET product_id=".$id." WHERE id='".$store_id."'
			")->execute();
		Yii::app()->clientScript->registerMetaTag('no-cache', nil, 'Pragma');
		echo 'El producto esta en la Vitrina.';
	}
	public function actionDelete($id)
	{	
		Yii::app()->db->createCommand("
				UPDATE product SET visible=false WHERE id=".$id."
			")->execute();
		Yii::app()->clientScript->registerMetaTag('no-cache', nil, 'Pragma');
		echo 'El producto se ha eliminado.';
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}